<?php
/**
 * --------------------------------
 * # 父类控制器-抽象类
 * --------------------------------
 */
namespace phpless\core;

abstract class Controller
{
    protected $view_data=[];
    public function __construct(){}
    public function assign($key,$value)
    {
        $this->view_data[$key]=$value;
    }
    // display
    public function display($tpl,$data=array())
    {
        if(!is_array($data))
            $data=(array)$data;
        $vars = get_object_vars($this);
        $view_data=$this->view_data;
        $view_data=array_merge($view_data,$vars,$data);
        // $view_data=array_filter($view_data);
        $View = new View($tpl,$view_data);
    }
    // 重定向
    public function redirect($url,$data='')
    {
        if(empty($url))
            return false;
        $jump_url=url($url,$data);
        redirect($jump_url);
    }
    public function success($info='操作成功！',$jump_url='',$jump_time=3)
    {
        $data=['info'=>$info,'status'=>1,'jump_url'=>$jump_url,'jump_time'=>$jump_time];
        $info_file=PHPLESS_TEMPLATE.DIRECTORY_SEPARATOR.'info.php';
        include $info_file;
    }
    public function error($info='操作失败！',$jump_url='',$jump_time=3)
    {
        $data=['info'=>$info,'status'=>0,'jump_url'=>$jump_url,'jump_time'=>$jump_time];
        $info_file=PHPLESS_TEMPLATE.DIRECTORY_SEPARATOR.'info.php';
        if(PHP_SAPI === 'cli'){
            $info_file=PHPLESS_TEMPLATE.DIRECTORY_SEPARATOR.'cli.php';
        }
        include $info_file;
    }
    protected function ajaxReturn($data=null,$info='请求成功',$status=1,$code = 200)
    {
        $result = [
            'status' => $status,
            'info'  => $info,
            'data' => $data,
            'code' => $code,
            'time' => time(),
        ];
        header("Content-type:Application/json;charset=utf-8");
        exit(json_encode($result));
    }
    // 不存在的方法是
    public function __call($methodName,$argument=null)
    {
        $this->error('您所访问页面不存在！');
        exit();
    }
}